//
//  oldRegisterParentsView.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/26.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldRegisterParentsView.h"
#import "UIImageView+oldImageView.h"
#import "oldNetWork.h"
#import "oldRegex.h"
#import "oldHub.h"

@interface oldRegisterParentsView()
{
    NSUInteger next_time;
}
@property (weak, nonatomic) IBOutlet UIImageView *img_avatar;
@property (weak, nonatomic) IBOutlet UILabel *label_name;
@property (weak, nonatomic) IBOutlet UILabel *label_sex;
@property (weak, nonatomic) IBOutlet UILabel *label_age;
@property (weak, nonatomic) IBOutlet UITextField *text_sms;
@property (weak, nonatomic) IBOutlet UITextField *text_relation;
@property (weak, nonatomic) IBOutlet UIButton *btn_bind;
@property (weak, nonatomic) IBOutlet UIButton *btn_sms;
@property (nonatomic, strong)NSTimer *timer;
@end

@implementation oldRegisterParentsView

- (void)awakeFromNib
{
    [super awakeFromNib];
    _btn_bind.layer.cornerRadius = 6.f;
    [_btn_bind addTarget:self action:@selector(btn_bind_tapped) forControlEvents:UIControlEventTouchDown];
    _btn_sms.layer.cornerRadius = 6.f;
    [_btn_sms addTarget:self action:@selector(sendSMS) forControlEvents:UIControlEventTouchDown];
    
    _img_avatar.layer.cornerRadius = 30.f;
}

- (void)setParent:(oldServer *)parent
{
    _parent = parent;
    [_img_avatar showHeadImg:parent.avatar];
    _label_name.text = parent.uname;
    _label_sex.text = [parent getAccountSex];
    _label_age.text = [parent getNStringOfAge];
}

- (void)btn_bind_tapped
{
    [self.delegate sendBind:_text_sms.text relation:_text_relation.text];
}

- (void)sendSMS
{
    if (![oldRegex isMobileNo:_phoneNum]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    [self delayForGetCode];
    
    [oldHub waiting:@"验证码发送中"];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:_phoneNum ,@"mobile", @"1", @"role", @"4", @"method", nil];
    
    [OLDNetWork httpRequestWithPostPut:URL_Register_SMS params:dict success:^(NSDictionary *dic) {
        [oldHub hideHUD];
    } error:^(NSString *error) {
        [oldHub hideHUD];
        [oldHub warningUser:error];
    }];
}

#pragma mark - timer
- (void)delayForGetCode
{
    next_time = 60;
    
    [self SmsCanClick:NO];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setDelay:) userInfo:_timer repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
    [_timer fire];
}

- (void)setDelay:(id)sender
{
    next_time--;
    if (next_time == 0) {
        [self SmsCanClick:YES];
        
        _timer.fireDate = [NSDate distantFuture];
        [_timer invalidate];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"重新发送(%ld)", (long)next_time];
    _btn_sms.titleLabel.text = str;
    [_btn_sms setTitle:str forState:UIControlStateNormal];
}

- (void)SmsCanClick:(BOOL)can
{
    if (can) {
        _btn_sms.userInteractionEnabled = YES;
        _btn_sms.alpha = 1.0;
        _btn_sms.titleLabel.text = @"获取验证码";
        [_btn_sms setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else
    {
        _btn_sms.userInteractionEnabled = NO;
        _btn_sms.alpha = 0.4;
    }
}

@end
